﻿//
//    ___ _____ ___  ___ __  __ 
//   / __|_   _/ _ \| _ \  \/  |
//   \__ \ | || (_) |   / |\/| |
//   |___/ |_| \___/|_|_\_|  |_|
// 
//   Storm.TabControl.dll
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//     Storm.TabControl.dll was created under the LGPL license.
//     It was based on another .dll and evolved from that one.
//     The original .dll was created by Hadi Eskandari.
//
//   What is Storm?
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//     Storm is a set of dynamic link libraries used in Theodor
//     "Vestras" Storm Kristensen's application "Moonlite".
//     
//
//   Thanks:
//   ¯¯¯¯¯¯¯
//     - Hadi Eskandari for creating and publishing the library.
//
//
//   Copyright (c) Theodor "Vestras" Storm Kristensen 2009
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//

namespace Storm.TabControl
{
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.ComponentModel;
    using Storm.TabControl.Design;

    [Designer(typeof(TabStripItemDesigner))]
    [ToolboxItem(false)]
    [DefaultProperty("Title")]
    [DefaultEvent("Changed")]
    public class TabStripItem : Panel
    {
        #region Events
        public event EventHandler Changed;
        #endregion

        #region Members
        private RectangleF stripRect = Rectangle.Empty;
        private Image image = null;
        private bool canClose = true;
        private bool selected = false;
        private bool visible = true;
        private bool isDrawn = false;
        private bool isHot = false;
        private string title = string.Empty;
		private TabStrip tabStripParent = null;
        #endregion

        #region Properties
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }

		public TabStrip TabStripParent
		{
			get { return tabStripParent; }
			set { tabStripParent = value; }
		}

        [DefaultValue(true)]
        public new bool Visible
        {
            get { return visible; }
            set
            {
                if (visible == value)
                    return;

                visible = value;
                OnChanged();
            }
        }

        internal RectangleF StripRect
        {
            get { return stripRect; }
            set { stripRect = value; }
        }

        [DefaultValue(false)]
        public bool IsHot
        {
            get { return isHot; }
            set { isHot = value; }
        }

        [Browsable(false)]
        [DefaultValue(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool IsDrawn
        {
            get { return isDrawn; }
            set
            {
                if (isDrawn == value)
                    return;

                isDrawn = value;
            }
        }

        [DefaultValue(null)]
        public Image Image
        {
            get { return image; }
            set { image = value; }
        }

        [DefaultValue(true)]
        public bool CanClose
        {
            get { return canClose; }
            set { canClose = value; }
        }

        [DefaultValue("Name")]
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                if (title == value)
                    return;

                title = value;
                OnChanged();
            }
        }

        [DefaultValue(false)]
        [Browsable(false)]
        public bool Selected
        {
            get { return selected; }
            set
            {
                if (selected == value)
                    return;

                selected = value;
            }
        }

        [Browsable(false)]
        public string Caption
        {
            get { return Title; }
        }
        #endregion

        #region Ctor
        public TabStripItem()
            : this(string.Empty, null)
        {
        }

        public TabStripItem(Control displayControl)
            : this(string.Empty, displayControl)
        {
        }

        public TabStripItem(string caption, Control displayControl)
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ContainerControl, true);

            selected = false;
            Visible = true;

            UpdateText(caption, displayControl);

            if (displayControl != null)
                Controls.Add(displayControl);
        }
        #endregion

        #region IDisposable
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                if (image != null)
                    image.Dispose();
            }
        }
        #endregion

        #region ShouldSerialize
        public bool ShouldSerializeIsDrawn()
        {
            return false;
        }

        public bool ShouldSerializeDock()
        {
            return false;
        }

        public bool ShouldSerializeControls()
        {
            return Controls != null && Controls.Count > 0;
        }

        public bool ShouldSerializeVisible()
        {
            return true;
        }
        #endregion

        #region Methods
		#region Private
		private void UpdateText(string caption, Control displayControl)
        {
            if (displayControl != null && displayControl is ICaptionSupport)
            {
                ICaptionSupport capControl = displayControl as ICaptionSupport;
                Title = capControl.Caption;
            }
            else if (caption.Length <= 0 && displayControl != null)
                Title = displayControl.Text;
            else if (caption != null)
                Title = caption;
            else
                Title = string.Empty;
		}

		private void OnControlMouseEnter(object sender, EventArgs e)
		{
			isHot = false;
			tabStripParent.CheckHot();
		}
		#endregion

		#region Public
		public void Assign(TabStripItem item)
        {
            Visible = item.Visible;
            Text = item.Text;
            CanClose = item.CanClose;
            Tag = item.Tag;
        }

        public override string ToString()
        {
            return Caption;
		}
		#endregion

		#region Protected
		protected internal virtual void OnChanged()
		{
			if (Changed != null)
				Changed(this, EventArgs.Empty);
		}

		protected override void OnControlAdded(ControlEventArgs e)
		{
			e.Control.MouseEnter += OnControlMouseEnter;
			base.OnControlAdded(e);
		}

		protected override void OnMouseEnter(EventArgs e)
		{
			isHot = false;
			tabStripParent.CheckHot();
			base.OnMouseEnter(e);
		}
		#endregion
		#endregion
	}
}